<template>
  <div class="container">
    <el-card>
      <!-- 学科 -->
      <div class="el-row">
        <!-- 左边 -->
        <div class="el-col el-col-18">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="学科名称" size="small">
              <el-input v-model="formInline.subjectName"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="plain" size="small" @click="clearSubject"
                >清除</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="searchSubject"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <!-- 右边 -->
        <div class="el-col el-col-6" style="text-align: right">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="addSubject('add')"
          >
            新增学科
          </el-button>
        </div>
      </div>
      <!-- 显示数据 -->
      <el-alert
        :title="`一共 ${pagingData.total} 条数据`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="subjectList" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="140">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="120">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="120">
          <template slot-scope="{ row }">
            {{ row.isFrontDisplay === 1 ? "是" : "不是" }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="address" tags="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column prop="address" label="操作" width="300">
          <template slot-scope="{ row }">
            <el-link
              type="primary"
              :underline="false"
              @click="subjectsClass(row)"
              >学科分类</el-link
            >
            <el-link
              type="primary"
              :underline="false"
              style="margin-left: 3px"
              @click="subjectsTag(row)"
              >学科标签</el-link
            >
            <el-link
              type="primary"
              :underline="false"
              style="margin-left: 3px"
              @click="editBtn(row, 'edit')"
              >修改</el-link
            >
            <el-link
              type="primary"
              :underline="false"
              style="margin-left: 3px"
              @click="delBtn(row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 放分页组件 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :page-sizes="pagingData.pageSizes"
          :page-size="pagingData.pageSize"
          layout="prev, pager, next, sizes, jumper"
          :total="pagingData.total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </el-row>
      <el-dialog
        @close="addBtnCancel"
        :visible.sync="showSubjectDialog"
        :title="type === 'edit' ? '编辑学科' : '新增学科'"
        width="33%"
        class="demo-ruleForm"
      >
        <el-form
          label-width="80px"
          :model="subjectsObj"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              placeholder="请输入学科名称"
              width="440"
              v-model="subjectsObj.subjectName"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否展示">
            <el-switch
              active-color="#13ce66"
              inactive-color="#ff4949"
              v-model="subjectsObj.isFrontDisplay"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <el-row>
          <el-col :span="10" :offset="16">
            <el-button @click="addBtnCancel" size="small">取消</el-button>
            <el-button type="primary" @click="addBtnOk" size="small"
              >确认</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      showSubjectDialog: false, // 弹层的默认显示
      type: null, // 判断是新增还是编辑
      subjectsObj: { // 弹层添加学科数据
        subjectName: '', // 学科名称,
        isFrontDisplay: '' // 是否展示
      },
      formInline: {
        subjectName: '' // 搜索名称
      },
      // 校验规则
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      },
      subjectList: [], // 学科列表
      // 提交服务器的分页数据
      requestParameters: {
        page: '', // 当前页数
        pagesize: '', // 每页展示数量
        subjectName: '' // 学科名称
      },
      pagingData: {
        pageSize: 10, //  每页展示的数据
        pageSizes: [5, 10, 20, 50], // 分页每页展示
        total: 0 // 总条数
      }
    }
  },
  created () {
    this.getSubjectList()
  },
  methods: {
    async getSubjectList () {
      const { data } = await list(this.requestParameters)
      // console.log(data.item)
      data.items.forEach(item => {
        item.addDate = dayjs(item.addDate).format('YYYY-MM-DD hh:mm:ss')
        // console.log(item.addDate)
      })
      this.pagingData.total = data.counts
      this.subjectList = data.items
    },
    handleCurrentChange (page) {
      // console.log(page)
      this.requestParameters.page = page
      this.getSubjectList()
    },
    handleSizeChange (val) {
      // console.log(val)
      this.requestParameters.pagesize = val
      this.getSubjectList()
    },
    addSubject (type) {
      this.type = type
      this.showSubjectDialog = true
    },
    // 确定按钮
    async addBtnOk () {
      await this.$refs.ruleForm.validate()
      if (this.subjectsObj.isFrontDisplay) {
        this.subjectsObj.isFrontDisplay = 1
      } else {
        this.subjectsObj.isFrontDisplay = 0
      }
      if (this.type === 'add') {
        await add(this.subjectsObj)
        this.getSubjectList()
        this.$message.success('学科增加成功!')
        this.addBtnCancel()
      } else {
        await update(this.subjectsObj)
        this.addBtnCancel()
        this.getSubjectList()
        this.$message.success('学科修改成功!')
      }
    },
    // 取消按钮
    addBtnCancel () {
      this.subjectsObj.subjectName = ''
      this.subjectsObj.isFrontDisplay = true
      this.showSubjectDialog = false
      this.$refs.ruleForm.resetFields()
    },
    // 删除方法
    async delBtn (id) {
      try {
        await this.$confirm('您确定要删除此学科吗?', '操作提示', { type: 'warning' })
        await remove({ id })
        this.getSubjectList()
        this.$message.success('学科信息删除成功!')
      } catch (e) {
        console.log(e)
      }
    },
    // 编辑方法
    async editBtn (row, type) {
      this.type = type
      this.subjectsObj.id = row.id
      this.subjectsObj.subjectName = row.subjectName
      if (row.isFrontDisplay === 1) {
        this.subjectsObj.isFrontDisplay = true
      } else {
        this.subjectsObj.isFrontDisplay = false
      }
      this.showSubjectDialog = true
    },
    // 学科分类
    subjectsClass (row) {
      var url = `directorys?id=${row.id}&name=${row.subjectName}`
      this.$router.push({ path: url })
    },

    // 学科标签
    subjectsTag (row) {
      console.log(row)
      var url = `tags?id=${row.id}&name=${row.subjectName}`
      this.$router.push({ path: url })
    },
    // 搜索学科
    searchSubject () {
      this.requestParameters.subjectName = this.formInline.subjectName
      this.getSubjectList()
    },
    // 清除学科
    clearSubject () {
      this.formInline.subjectName = ''
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 0 10px;
  margin: 10px 0;
}
.el-card__body {
  padding: 20px;
  .el-col-18 {
    width: 75%;
  }
}
.el-row {
  margin-top: 20px;
}
.el-link--primary {
  margin-right: 8px;
}
</style>
